package com.wcy;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;

public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/sr-db", "root", "root")
                .globalConfig(builder -> {
                    builder.author("吴传亿") // 设置作者
                            .outputDir("E:\\CollegeProject\\StudyResource\\cloud-provider-user\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com") // 设置父包名
                                .moduleName("wcy") // 设置父包模块名
                                .entity("entity")
                                .service("service")
                                .serviceImpl("service.impl")
                                .mapper("mapper")
                                .controller("controller")
//                                .pathInfo(Collections.singletonMap(OutputFile.xml, "E:\\CollegeProject\\GymCourseRecommend\\src\\main\\java")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("user","user_collection","user_comment","user_feedback","user_learning_history","user_rating","user_role")
                                .entityBuilder()
                                .enableLombok()
                                .enableChainModel()
                                .disableSerialVersionUID()
                                .enableRemoveIsPrefix()
                                .enableFileOverride()
                                .logicDeleteColumnName("is_deleted")       // 设置需要生成的表名
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
